﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

public class VideoController : MonoBehaviour
{
    //设置 VideoPlayer、RawImage 和当前播放视频索引参数
    //videoPlayer 和 rawImage 不需要拖动赋值，直接 GetComponent() 即可
    private VideoPlayer mVideoPlayer;
    private RawImage mRawImage;
    private GameObject mParentImageTargetVideo;
    private GameObject mParentCanvasObj;

    //设置相关文本和按钮参数以及视频列表
    private Text mTextPlayOrPause;
    private Button mBtnVideoPlayOrPause;
    //public Button button_Pre;
    //public Button button_Next;

    public VideoClip[] videoClips;
    private int currentClipIndex = 0;
    private bool mIsTrackingFound = false;

    void Awake()
    {
        //获取VideoPlayer和RawImage组件，以及初始化当前视频索引
        mVideoPlayer = this.GetComponent<VideoPlayer>();
        mRawImage = this.GetComponent<RawImage>(); //获取不到也没关系，后边也不使用

        mBtnVideoPlayOrPause = (Button)GameObject.Find("Canvas/VideoControlButton").GetComponent<Button>();
        mTextPlayOrPause = mBtnVideoPlayOrPause.transform.Find("Text").GetComponent<Text>();
        mParentImageTargetVideo = GameObject.Find("ImageTargetVideo");
        mParentCanvasObj = GameObject.Find("ImageTargetVideo/Canvas");
        LogUtils.debug("VideoController Awake() mParentCanvasObj=" + mParentCanvasObj);
    }

    // Use this for initialization
    void Start()
    {
        LogUtils.debug(" VideoController Start() ");

        //设置相关按钮监听事件
        mBtnVideoPlayOrPause.onClick.AddListener(OnPlayOrPauseBtnClick);
        mBtnVideoPlayOrPause.gameObject.SetActive(false);
        //button_Pre.onClick.AddListener(OnPreVideo);
        //button_Next.onClick.AddListener(OnNextVideo);

        //为了刚开始不自动播放
        PauseVideo();
        SystemUtil.SetGameObjectChildrenInVisible(mParentImageTargetVideo);
    }

    // Update is called once per frame
    void Update()
    {
        //没有视频则返回，不播放
        if (mVideoPlayer.texture == null)
        {
            return;
        }
        //渲染视频到UGUI上, 因为在 Inspector 中共用了一个 RenderTexture，所以这里注释了也没关系
        //rawImage.texture = videoPlayer.texture;
    }

    public void OnTrackingFoundInVC()
    {
        LogUtils.debug("VideoController OnTrackingFound()");
        ToastUtil.showToast("VideoController OnTrackingFoundInVC()");
        mIsTrackingFound = true;

        //设置播放/暂停按钮可见
        mBtnVideoPlayOrPause.gameObject.SetActive(true);

        //设置 VideoPlayer 可见
        SystemUtil.SetGameObjectChildrenVisible(mParentImageTargetVideo);
    }

    public void OnTrackingLostInVC()
    {
        LogUtils.debug("VideoController OnTrackingLost()");
        mIsTrackingFound = false;

        //设置播放/暂停按钮不可见
        mBtnVideoPlayOrPause.gameObject.SetActive(false);

        //停止播放视频
        PauseVideo();
        SystemUtil.SetGameObjectChildrenInVisible(mParentImageTargetVideo);
    }

    public void OnPlayOrPauseBtnClick()
    {
        if (mIsTrackingFound)
        {
            ToastUtil.showToast("点击了按钮，并且找到了图片，开始播放/暂停视频");
            SystemUtil.SetGameObjectChildrenVisible(mParentImageTargetVideo);
            PlayOrPauseVideo();
        }
        else
        {
            ToastUtil.showToast("点击了按钮，但是没有找到图片，无法播放/暂停视频");
        }
    }

    /// <summary>
    /// 播放和暂停当前视频
    /// </summary>
    private void PlayOrPauseVideo()
    {
        LogUtils.debug("OnPlayOrPauseVideo videoPlayer.isPlaying= " + mVideoPlayer.isPlaying);

        //判断视频播放情况，播放则暂停，暂停就播放，并更新相关文本
        if (mVideoPlayer.isPlaying == true)
        {
            PauseVideo();
        }
        else
        {
            PlayVideo();
        }
    }

    private void PlayVideo()
    {
        mVideoPlayer.Play();
        mTextPlayOrPause.text = "暂停视频";
    }

    private void PauseVideo()
    {
        mVideoPlayer.Pause();
        mTextPlayOrPause.text = "播放视频";
    }


    /// <summary>
    /// 切换上一个视频
    /// </summary>
    //
    //private void OnPreVideo()
    //{

    //    //视频列表减一播放上一个视频，并进行避免越界操作
    //    currentClipIndex -= 1;
    //    if (currentClipIndex < 0)
    //    {
    //        currentClipIndex = videoClips.Length - 1;
    //    }
    //    mVideoPlayer.clip = videoClips[currentClipIndex];
    //    mTextPlayOrPause.text = "暂停";
    //}

    /// <summary>
    /// 切换下一个视频
    /// </summary>
    /// 
    //private void OnNextVideo()
    //{

    //    //视频列表加一播放下一个视频，并进行避免越界操作
    //    currentClipIndex += 1;
    //    currentClipIndex = currentClipIndex % videoClips.Length;
    //    mVideoPlayer.clip = videoClips[currentClipIndex];
    //    mTextPlayOrPause.text = "暂停";
    //}
}
